package com.woniuxy.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.user.entity.User;
import com.woniuxy.user.mapper.UserMapper;
import com.woniuxy.user.service.FileService;
import com.woniuxy.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wf
 * @since 2021-03-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private FileService fileService;

    @Override
    public Map<String, String> accountAndPassword(String account) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        User user = userMapper.selectOne(wrapper);
        Map<String,String> map = new HashMap<>();
        if(user != null){
            map.put("account",user.getAccount());
            map.put("password", user.getPassword());
            return map;
        }
        return null;
    }

    @Override
    public boolean changIcon(String account, MultipartFile file) {
        ResponseResult<?> responseResult = fileService.upload(file);
        if(responseResult.getCode() == 200){
            if(userMapper.changIcon(account,(String) responseResult.getData()) > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean changPassword(String account, String password) {
        return userMapper.changPassword(account,password)>0;
    }


}
